AnnieRoot.shootPeople = AnnieRoot.shootPeople || {};
AnnieRoot.shootPeople.ShootPeople = class extends annie.Sprite {
    constructor() {
        super();
        let s = this;
        /*_a2x_need_start*/
        s.bg_mc = null;
        s.eme = null;
        s.goldcoin = null;
        s.life_mc = null;
        s.people = null;
        s.skill1 = null;
        s.skill2 = null;
        s.skill3 = null;/*_a2x_need_end*/
        annie.initRes(s, "shootPeople", "ShootPeople");
        s.sheSound = new annie.Sound(annie.getResource("shootPeople", "She"));
        s.billionSheSound = new annie.Sound(annie.getResource("shootPeople", "Ds"));
        s.hitSound = new annie.Sound(annie.getResource("shootPeople", "Hit"));
        s.skill1Sound = new annie.Sound(annie.getResource("shootPeople", "Skill1"));
        s.skill3Sound = new annie.Sound(annie.getResource("shootPeople", "Skill3"));
        s.timeList = [0, 5, 0];
        let life = 1;//生命
        let stageWdith = 0;
        let stageAllWidth = 0;
        s.currentObjDevil = AnnieRoot.shootPeople.DevilManager;
        s.currentObjArrow = AnnieRoot.shootPeople.ArrowManager;
        s.currentObjMonster = AnnieRoot.shootPeople.MonsterManager;
        s.addEventListener(annie.Event.ADD_TO_STAGE, function (e) {
            AnnieRoot.score = 0;
            s.bg_mc.scaleX=s.bg_mc.scaleY=s.stage.viewRect.height/1334;
            s.goldcoin.y=s.stage.viewRect.y+30;
            s.life_mc.y=s.stage.viewRect.y+30;
            s.count = 0;
            s.goldcoin.fraction_txt.text = 0;
            s.initGameData();
            life = 1;
            for (let i = 1; i < 4; i++) {
                s["skill" + i].mouseChildren = false;
                s["skill" + i].gotoAndStop(1);
                s["skillStatus" + i] = false;
                s["skillCoolingTime" + i] = s.timeList[i - 1];
            }
            s.bg_mc.scaleX = s.bg_mc.scaleY = s.stage.viewRect.height / 1334;
            stageWdith = s.stage.viewRect.x;
            stageAllWidth = s.stage.viewRect.width;
            for (var i = 1; i < 4; i++) {
                s["skill" + i].cooling_txt.visible = false;
            }
            s.isStart = true;
            s.people.gong.rotation = 130;
            timer.reset();
            timer.start();
        });

        s.addEventListener(annie.MouseEvent.MOUSE_UP, function (e) {
            s.gongRotationHandler(e.stageY, e.stageX);
            if (e.target.name.indexOf("skill") != -1) {
                s.ArrowSkill(e.target.name);
            }
        });
        var maxDis = 1000;
        s.addEventListener(annie.Event.ENTER_FRAME, function (e) {
            if (s.isStart) {
                ////enemy
                var enemy = null;
                for (var i = s.enemyList.length - 1; i >= 0; i--) {//生成小恶魔
                    enemy = s.enemyList[i];
                    if (enemy.motion == "x") {
                        enemy.x -= enemy.speed;
                        enemy.scaleX = 1;
                    } else {
                        enemy.scaleX = -1;
                        enemy.x += enemy.speed;
                    }
                    if (enemy.x < -100 || enemy.x > 800) {
                        if (enemy.name == "devil") {
                            s.currentObjDevil.recoveryEnemy(enemy);
                            life++;
                            s.life_mc.gotoAndStop(life);
                            if (life > 3) {
                                s.gameOver()
                            }
                        } else if (enemy.name == "monster") {
                            s.currentObjMonster.recoveryEnemy(enemy);
                        }
                        s.enemyList.splice(i, 1);
                        s.removeChild(enemy);
                    }
                }
                var arrow = null;
                for (var i = s.arrowList.length - 1; i >= 0; i--) {//生成箭矢
                    arrow = s.arrowList[i];
                    arrow.y = arrow.startY + Math.sin(arrow.direction) * arrow.dis;
                    arrow.x = arrow.startX + Math.cos(arrow.direction) * arrow.dis;
                    arrow.dis += arrow.speed;
                    if (arrow.type == 4) {
                        maxDis = 200;
                    } else {
                        maxDis = 800;
                    }
                    if (arrow.dis > maxDis) {
                        s.currentObjArrow.recoveryBullet(arrow);
                        s.arrowList.splice(i, 1);
                        s.removeChild(arrow);
                    }
                }
                ///hit
                var isHit = false;
                var cArrowList = [];
                for (var i = s.enemyList.length - 1; i >= 0; i--) {
                    enemy = s.enemyList[i];
                    isHit = false;
                    for (var j = s.arrowList.length - 1; j >= 0; j--) {
                        arrow = s.arrowList[j];
                        if (annie.Point.distance(enemy.x, enemy.y, arrow.x, arrow.y) < 40) { //碰撞检测
                            s.hitSound.play();
                            if (enemy.scaleX == -1) {
                                enemy.gotoAndStop(3);
                                enemy.b.play();
                            } else {
                                enemy.gotoAndStop(2);
                                enemy.m.play();
                            }
                            if (arrow.type != 1) {
                                if (arrow.type == 3) {
                                    s.skill3Sound.play();
                                    for (var m = 0; m < 6; m++) {
                                        var bullet = s.currentObjArrow.getArrow();
                                        bullet.direction = m * Math.PI / 3;
                                        bullet.x = arrow.x;
                                        bullet.y = arrow.y;
                                        bullet.startX = arrow.x;
                                        bullet.startY = arrow.y;
                                        bullet.rotation = m * 60;
                                        bullet.speed = 20;
                                        bullet.dis = 0;
                                        bullet.type = 4;
                                        bullet.gotoAndStop(3);
                                        s.addChild(bullet);
                                        cArrowList.push(bullet);
                                        s.people.gong.gotoAndPlay(2);
                                    }
                                }
                                s.currentObjArrow.recoveryBullet(arrow);
                                s.arrowList.splice(j, 1);
                                s.removeChild(arrow);
                            }
                            isHit = true;
                            //hit success
                            break;
                        }
                    }
                    if (isHit) {
                        var emee = s.enemyList.splice(i, 1);

                        if (emee[0].name == "devil") {
                            s.count += 5;
                            if (emee[0].scaleX == 1) {
                                s.currentObjDevil.recoveryEnemy(emee[0], "m");
                            } else {
                                s.currentObjDevil.recoveryEnemy(emee[0], "b");
                            }
                        } else if (emee[0].name == "monster") {
                            s.count += 1;
                            if (emee[0].scaleX == 1) {
                                s.currentObjMonster.recoveryEnemy(emee[0], "m");
                            } else {
                                s.currentObjMonster.recoveryEnemy(emee[0], "b");
                            }
                        }
                        // s.removeChild(e.target.parent);
                        s.goldcoin.fraction_txt.text = s.count;

                    }
                }
                s.arrowList = s.arrowList.concat(cArrowList);
                s.timeCount++;
                s.monsterTime++;
                if (s.timeCount >= 30) {
                    s.timeCount = 0;
                    var monster = s.currentObjMonster.getEnemy();
                    if (Math.random() > 0.5) {
                        monster.motion = "x";
                        monster.x = 700;
                    } else {
                        monster.motion = "y";
                        monster.x = -100;
                    }
                    monster.visible = true;
                    monster.speed = Math.random() * 5 + 4;
                    monster.y = Math.random() * 500 + 100;
                    s.addChild(monster);
                    s.enemyList.push(monster);
                }
                if (s.monsterTime >= 300) {
                    s.monsterTime = 0;
                    var smallEnemyDevil = s.currentObjDevil.getEnemy();
                    smallEnemyDevil.speed = Math.random() * 5 + 4;
                    if (Math.random() > 0.5) {
                        smallEnemyDevil.motion = "x";
                        smallEnemyDevil.x = 700;
                    } else {
                        smallEnemyDevil.motion = "y";
                        smallEnemyDevil.x = -100
                    }
                    smallEnemyDevil.visible = true;
                    smallEnemyDevil.y = Math.random() * 500 + 100;
                    s.addChild(smallEnemyDevil);
                    s.enemyList.push(smallEnemyDevil);
                }
                if (s.arrowTime < 8) {
                    s.arrowTime++;
                }
            }
            s.gongRotationHandler()
        });
        var timer = new annie.Timer(500);
        timer.addEventListener(annie.Event.TIMER, function (e) {
            s.skillCoolingHandler(s.arrowType);
        });

    }

    //技能选中
    ArrowSkill(e) {
        var s = this;
        for (var i = 1; i <= 3; i++) {
            if ((e.charAt(e.length - 1)) == i) {
                if (!s["skillStatus" + i]) {
                    s["skill" + i].gotoAndStop(2);
                    s.arrowType = i;
                    s["skillCoolingTime" + i] = s.skillCoolingArr[i - 1];
                }
            }
        }
        s.launchArrow();
    };

    gongRotationHandler(y, x) {
        var s = this;
        if (!s.rotaionFalse) {
            s.zzl -= 4;
        } else {
            s.zzl += 4;
        }

        s.people.gong.rotation = s.zzl;
        var rotationAbs = Math.abs(s.people.gong.rotation);
        if (rotationAbs > 150) {
            s.rotaionFalse = true;
        } else if (rotationAbs <= 0) {
            s.rotaionFalse = false;
        }
        s.gongRotation = s.zzl;


    };

    addArrowHandler(pointRadian, rotation, type) {
        var s = this;
        if (type > 1) {
            s["skillStatus" + (type - 1)] = true;
        }
        // console.log(s.arrowType)
        var bullet = s.currentObjArrow.getArrow();
        bullet.direction = pointRadian;
        bullet.x = s.people.x + s.people.gong.x;
        bullet.y = s.people.y + s.people.gong.y;
        bullet.startX = s.people.x + s.people.gong.x;
        bullet.startY = s.people.y + s.people.gong.y;
        bullet.rotation = rotation;
        bullet.speed = 25;
        bullet.dis = 0;
        bullet.type = s.arrowType;
        s.addChild(bullet);
        s.arrowList.push(bullet);
        s.people.gong.gotoAndPlay(2);
        if (type == 3) {
            type = 1;
        }
        bullet.gotoAndStop(type);
    };

    gameOver() {
        var s = this;
        s.isStart = false;
        AnnieRoot.score = s.count;
        annie.globalDispatcher.dispatchEvent("onShowContent", {sceneId: 2});
    };

    launchArrow() {
        var s = this;
        if (s.arrowTime == 8) {
            s.arrowTime = 0;
            if (s.arrowType == 2) {
                s.skill2.m.gotoAndPlay(2);
                s.billionSheSound.play();
                for (var i = 0; i < 7; i++) {
                    s.addArrowHandler((s.gongRotation + s.arrowAngleArr[i]) * Math.PI / 180, s.gongRotation + s.arrowAngleArr[i], 3)
                }
            } else if (s.arrowType == 1) {
                s.sheSound.play();
                s.skill1.m.gotoAndPlay(2);
                s.addArrowHandler(s.gongRotation * Math.PI / 180, s.gongRotation, s.arrowType + 1);
            } else if (s.arrowType == 3) {
                s.skill3.m.gotoAndPlay(2);
                s.sheSound.play();
                s.addArrowHandler(s.gongRotation * Math.PI / 180, s.gongRotation, s.arrowType + 1);
            } else {
            }


            s.arrowType = 0;
        }
    };

    skillCoolingHandler(type) {
        var s = this;
        if (s.skillStatus1) {
            if (s.skillCoolingTime1 > 0) {
                s.skillCoolingTime1--;
                s.skill1.cooling_txt.text = s.skillCoolingTime1;
            } else {
                s.skillStatus1 = false;
                s.skill1.cooling_txt.visible = false;

            }
        }
        if (s.skillStatus2) {
            if (s.skillCoolingTime2 > 0) {
                s.skillCoolingTime2--;
                s.skill2.cooling_txt.text = s.skillCoolingTime2;
            } else {
                s.skillStatus2 = false;
                s.skill2.cooling_txt.visible = false;
            }
        }
        if (s.skillStatus3) {
            if (s.skillCoolingTime3 > 0) {
                s.skillCoolingTime3--;
                s.skill3.cooling_txt.text = s.skillCoolingTime3;
            } else {
                s.skillStatus3 = false;
                s.skill3.cooling_txt.visible = false;
            }
        }
    };

    initGameData() {
        let s = this;
        if (s.enemyList && s.enemyList.length > 0) {
            for (let i = 0; i < s.enemyList.length; i++) {
                s.removeChild(s.enemyList[i]);
            }
        }
        if (s.arrowList && s.arrowList.length > 0) {
            for (let i = 0; i < s.arrowList.length; i++) {
                s.removeChild(s.arrowList[i]);
            }
        }
        s.enemyList = [];       //恶魔数组
        s.arrowList = [];       //弓箭数组
        s.arrowType = 0;        //箭初始技能状态
        s.isStart = true;       //游戏开关
        s.timeCount = 0;        //恶魔出现频率
        s.arrowTime = 0;        //弓箭频率
        s.gongRotation = 0;     //弓箭角度
        s.monsterTime = 0;      //怪兽出现频率
        s.skillCoolingArr = [1, 2, 2]; //技能冷却
        s.life_mc.gotoAndStop(1);
        s.arrowAngleArr = [0, 5, 10, 15, -5, -10, -15];
        s.count = 0;
        s.rotaionFalse = false;
        s.zzl = 0;
    }
};